﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericEntities;
using System.Collections;

namespace DiaryPlanner
{
    public partial class PlannerWeek : UserControl
    {
        Planner planner;

        List<Label> weekList = new List<Label>();
        List<Label> timeList = new List<Label>();
        List<Label> dayofWeekList = new List<Label>();

        const int week = 7;
        const int time = 24;
        const int half = 2;
        const int minute = 30;
        const int labelNum = week * time * half;
        const int labelTimeNum = time * half;

        DateTime curentDataTime;
        private int currentIndex = 0;
        private int currentTime = 0;

        System.Drawing.Color userColor = System.Drawing.Color.SkyBlue;
        System.Drawing.Color userColor1 = System.Drawing.Color.GreenYellow;

        public PlannerWeek()
        {
            InitializeComponent();
            initWeek();
        }

        private void initWeek()
        {
            CheckForIllegalCrossThreadCalls = false; //스레드 충돌에러 없애기

            int i;

            dayofWeekList.Add(weekLabel0);
            dayofWeekList.Add(weekLabel1);
            dayofWeekList.Add(weekLabel2);
            dayofWeekList.Add(weekLabel3);
            dayofWeekList.Add(weekLabel4);
            dayofWeekList.Add(weekLabel5);
            dayofWeekList.Add(weekLabel6);

            for (i = 0; i < labelNum; i++)
                weekList.Add(new System.Windows.Forms.Label());

            for (i = 0; i < labelTimeNum; i++)
                timeList.Add(new System.Windows.Forms.Label());

            this.SuspendLayout();

            for (i = 0; i < labelNum; i++)
            {
                weekList[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                weekList[i].Location = new System.Drawing.Point(0, 0);
                weekList[i].Margin = new System.Windows.Forms.Padding(0);
                weekList[i].Name = "labelW"+i.ToString();
                weekList[i].Size = new System.Drawing.Size(54, 25);
                weekList[i].Text = "";
            }

            for (i = 0; i < labelTimeNum; i++)
            {
                timeList[i].Location = new System.Drawing.Point(0, 0);
                timeList[i].Margin = new System.Windows.Forms.Padding(0);
                timeList[i].Name = "labelT" + i.ToString();
                timeList[i].Size = new System.Drawing.Size(53, 25);
                if (i % 2 == 0) timeList[i].Text = getTime_KR(i/2);
                else timeList[i].Text = "";
            }

            for (i = 0; i < labelNum; i++)
            {
                if (i % 7 == 0) this.flowLayoutPanel1.Controls.Add(timeList[i/7]);
                this.flowLayoutPanel1.Controls.Add(weekList[i]);
            }

            this.ResumeLayout(false);
            this.PerformLayout();

            //클릭 이벤트 핸들러 등록
            for (i = 0; i < weekList.Count; i++)
                weekList[i].Click += new System.EventHandler(this.weekList_Click);

            //더블 클릭 이벤트 핸들러 등록
            for (i = 0; i < weekList.Count; i++)
                weekList[i].DoubleClick += new System.EventHandler(this.weekList_DoubleClick);
        }

        public void viewWeek()
        {
            for (int i = 0; i < labelNum; i++)
            {
                weekList[i].Text = "";
            }
        }

        public void viewAllDB()
        {
            viewWeek();

            IList list = CalendarDB.Instance().loadAll_db();

            planner.plannerListClear();

            int select = 0;

            
            for (int i = 0; i < weekList.Count; i++)
            {
                if (weekList[i].BackColor == userColor)
                {
                    select = i;
                }
                else if (weekList[i].BackColor == userColor1)
                    weekList[i].BackColor = System.Drawing.Color.White;
            }
            

            for (int i = 0; i < list.Count; i++)
            {
                if (getDate().Year == ((GenericEvent)list[i]).StartTime.Year &&
                getDate().Month == ((GenericEvent)list[i]).StartTime.Month &&
                (curentDataTime.Day <= ((GenericEvent)list[i]).StartTime.Day && curentDataTime.AddDays(6).Day >= ((GenericEvent)list[i]).StartTime.Day))
                {
                    if(weekList[ ((int)((GenericEvent)list[i]).StartTime.DayOfWeek) 
                        + int.Parse(((GenericEvent)list[i]).StartTime.Hour.ToString()) * 7 * 2
                        + (int.Parse(((GenericEvent)list[i]).StartTime.Minute.ToString()) / minute)*7 ].BackColor != userColor)
                    weekList[ ((int)((GenericEvent)list[i]).StartTime.DayOfWeek) 
                        + int.Parse(((GenericEvent)list[i]).StartTime.Hour.ToString()) * 7 * 2
                        + (int.Parse(((GenericEvent)list[i]).StartTime.Minute.ToString()) / minute)*7 ].BackColor = userColor1;

                    weekList[((int)((GenericEvent)list[i]).StartTime.DayOfWeek)
                            + int.Parse(((GenericEvent)list[i]).StartTime.Hour.ToString()) * 7 * 2
                            + (int.Parse(((GenericEvent)list[i]).StartTime.Minute.ToString()) / minute) * 7].Text = ((GenericEvent)list[i]).Title;

                    if (getDate().Day.ToString() == ((GenericEvent)list[i]).StartTime.Day.ToString())
                    {
                        planner.plannerListAdd((GenericEvent)list[i]);
                    }
                }
            }
            
        }

        public void setWeekLabel()
        {
            int i;
            DateTime temp = planner.monthCalendar1.SelectionStart;

            switch (planner.monthCalendar1.SelectionStart.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    break;
                case DayOfWeek.Monday:
                    temp = temp.AddDays(-1);
                    break;
                case DayOfWeek.Tuesday:
                    temp = temp.AddDays(-2);
                    break;
                case DayOfWeek.Wednesday:
                    temp = temp.AddDays(-3);
                    break;
                case DayOfWeek.Thursday:
                    temp = temp.AddDays(-4);
                    break;
                case DayOfWeek.Friday:
                    temp = temp.AddDays(-5);
                    break;
                case DayOfWeek.Saturday:
                    temp = temp.AddDays(-6);
                    break;;
            }

            curentDataTime = new DateTime(temp.Ticks);

            for (i = 0; i < dayofWeekList.Count; i++)
            {
                dayofWeekList[i].Text = temp.Month.ToString() +"."+ temp.Day.ToString() + "(" + getWeekDay_Kr(temp) + ")";
                temp = temp.AddDays(1);
            }
        }

        private string getWeekDay_Kr(DateTime dateTime)
        {
            string weekString = "";

            switch (dateTime.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    weekString = "일";
                    break;
                case DayOfWeek.Monday:
                    weekString = "월";
                    break;
                case DayOfWeek.Tuesday:
                    weekString = "화";
                    break;
                case DayOfWeek.Wednesday:
                    weekString = "수";
                    break;
                case DayOfWeek.Thursday:
                    weekString = "목";
                    break;
                case DayOfWeek.Friday:
                    weekString = "금";
                    break;
                case DayOfWeek.Saturday:
                    weekString = "토";
                    break; ;
            }

            return weekString;
        }

        private string getTime_KR(int time)
        {
            string temp = "";

            if (time == 0)
                temp = "오전12시";
            else if (time < 12)
            {
                temp = "오전" + (time).ToString() + "시";
            }
            else if (time == 12)
                temp = "오후12시";
            else
            {
                temp = "오후" + (time - 12).ToString() + "시";
            }

            return temp;
        }

        public DateTime getDate()
        {
            return curentDataTime.AddDays(currentIndex);
        }

        private void weekList_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            for (int i = 0; i < weekList.Count; i++)
                weekList[i].BackColor = System.Drawing.Color.White;

            label.BackColor = userColor;

            setWeekLabel();

            for (int i = 0; i < weekList.Count; i++)
                if (weekList[i].BackColor == userColor)
                {
                    currentIndex = i % 7;
                    currentTime = i / 7;
                    break;
                }
            viewAllDB();
        }

        private void weekList_DoubleClick(object sender, EventArgs e)
        {
            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(planner, this);
            PlannerEditObject.ShowDialog();
            viewAllDB();
        }

        public string weekTime(int time)
        {
            for (int i = 0; i < weekList.Count; i++)
            {
                if (weekList[i].BackColor == userColor)
                {
                    if (time == 0) return getTime_colon(i/7);
                    else return getTime_colon(i/7 + 1);
                }
            }
            return null;
        }

        private string getTime_colon(int time)
        {
            string temp;

            if (time == 0) temp = "00";
            else temp = (time / 2).ToString();

            if (time % 2 == 0) temp = temp + ":" + ((time % 2) * 30).ToString() + "0";
            else temp = temp + ":" + ((time % 2) * 30).ToString();

            return temp;
        }

        public void initPlanner(Planner planner)
        {
            this.planner = planner;
            setWeekLabel();
        }

        public void setUnSelected()
        {
            for (int i = 1; i < weekList.Count; i++)
                if (weekList[i].BackColor == userColor)
                    weekList[i].BackColor = System.Drawing.Color.White;
        }
    }
}

